<template>
  <div class="add-depa">
    <el-dialog :title="mytitle" :visible="showDialog">
      <!-- 表单组件  el-form   label-width设置label的宽度   -->
      <!-- 匿名插槽 -->
      <el-form
        label-width="120px"
        :model="formData"
        :rules="rules"
        ref="myform"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input
            style="width:80%"
            placeholder="1-50个字符"
            v-model="formData.name"
          />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input
            style="width:80%"
            placeholder="1-50个字符"
            v-model="formData.code"
          />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            style="width:80%"
            placeholder="请选择"
            v-model="formData.manager"
          >
            <el-option
              v-for="item in people"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            >
            </el-option
          ></el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            style="width:80%"
            placeholder="1-300个字符"
            type="textarea"
            :rows="3"
            v-model="formData.introduce"
          />
        </el-form-item>
      </el-form>
      <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
      <el-row slot="footer" type="flex" justify="center">
        <!-- 列被分为24 -->
        <el-col :span="6">
          <el-button type="primary" size="small" @click="submit"
            >确定</el-button
          >
          <el-button size="small" @click="cancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDepartmentApi,
  addDeparrtmentApi,
  getDepartmentByIdApi,
  updateDeparmentApi
} from '../../../api/department'
import { getSimpleUserApi } from '../../../api/employees'
export default {
  name: 'adddepa',
  props: ['showDialog', 'currentObj'],
  data () {
    return {
      people: [],
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称在 1 到 50 个字符',
            trigger: 'blur'
          },
          {
            validator: async (rules, value, callback) => {
              const res = await getDepartmentApi()

              if (!this.formData.id) {
                var bool = res.depts
                  .filter(item => {
                    return item.pid === this.currentObj.id
                  })
                  .some(item => {
                    return item.name === value
                  })
              } else {
                var bool = res.depts
                  .filter(item => {
                    return (
                      item.pid === this.currentObj.id &&
                      item.id !== this.currentObj.id
                    )
                  })
                  .some(item => {
                    return item.name === value
                  })
              }
              !bool ? callback() : callback(new Error('当前部门下已有同名部门'))
            }
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码在 1 到 50 个字符',
            trigger: 'blur'
          },
          {
            //   验证value是否在所有数据元中code出现
            validator: async (rules, value, callback) => {
              const res = await getDepartmentApi()
              if (!this.formData.id) {
                var bool = res.depts.some(item => {
                  return item.code === value
                })
              } else {
                var bool = res.depts.some(item => {
                  return item.code === value && item.id !== this.currentObj.id
                })
              }
              !bool ? callback() : callback(new Error('当前code与其他部门冲突'))
            }
          }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门描述', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码在 1 到 50 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async getsimpleuser () {
      const res = await getSimpleUserApi()
      this.people = res
    },
    submit () {
      this.$refs.myform.validate(async res => {
        if (res) {
          // 新增
          if (!this.formData.id) {
            const res = await addDeparrtmentApi({
              ...this.formData,
              pid: this.currentObj.id
            })
            console.log(res)
            this.$message({
              type: 'success',
              message: '新增成功'
            })
            this.$emit('update:showDialog', false)
            this.$emit('refreshdata')
            // 清空添加数据源
            this.formData = {
              name: '', // 部门名称
              code: '', // 部门编号
              manager: '', // 部门负责人
              introduce: '' // 部门介绍
            }
            // 修改
          } else {
            await updateDeparmentApi(this.formData)
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            this.$emit('update:showDialog', false)
            this.$emit('refreshdata')
            // 清空添加数据源
            this.formData = {
              name: '', // 部门名称
              code: '', // 部门编号
              manager: '', // 部门负责人
              introduce: '' // 部门介绍
            }
          }
        }
      })
    },
    // 取消按钮取消时清空验证关闭弹框
    cancel () {
      this.$refs.myform.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getDepartmentById (id) {
      this.formData = await getDepartmentByIdApi(id)
    }
  },
  created () {
    this.getsimpleuser()
  },
  computed: {
    mytitle () {
      return !this.formData.id ? '新增' : '修改'
    }
  }
}
</script>

<style lang="scss" scoped>
// .add-depa {
// }
</style>
